<?php

Raise::load('core.RaiseFileSystem');

/**
 * TestCoreRaiseFileSystem class
 * Testing unit of 'phpraise/core/RaiseFileSystem.php'
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseFileSystem extends RaiseTest {
    
    public static function testClass(){
        self::assertClassExists('RaiseFileSystem');
        self::assertClassExtends('RaiseFileSystem', 'RaiseStaticClass');
    }

    public static function testFileExist(){
        self::assertTrue(RaiseFileSystem::fileExist('mocks/MockConfigFile.ini'));
        self::assertTrue(RaiseFileSystem::fileExist('index.php'));
        self::assertFalse(RaiseFileSystem::fileExist('path_to/Nowhere'));
        self::assertFalse(RaiseFileSystem::fileExist('path_test\Nowhere.bin'));
    }

    public static function testCreateFile(){
        $fp = RaisePath::combine(RaisePath::tempPath(), 'tmpFile.bin');
        $file = RaiseFileSystem::createFile($fp);
        self::assertObjectClass($file, 'RaiseFile');
        self::assertTrue($file->exists());
        $file->delete();
        self::assertFalse($file->exists());

        $file = RaiseFileSystem::createFile($fp, 'someContent');
        self::assertObjectClass($file, 'RaiseFile');
        self::assertTrue($file->exists());
        self::assertStrictEqual($file->getContent(), file_get_contents($fp));
        $file->delete();
        self::assertFalse($file->exists());
    }

    public static function testDeleteFile(){
        $fp = RaisePath::combine(RaisePath::tempPath(), 'tmpFile.bin');
        $file = RaiseFileSystem::createFile($fp);
        self::assertTrue($file->exists());
        RaiseFileSystem::deleteFile($fp);
        self::assertFalse($file->exists());
    }

    public static function testCreateDeletePath(){
        $fp = RaisePath::combine(RaisePath::tempPath(), 'testPath');
        $p = RaiseFileSystem::createPath($fp);
        self::assertTrue($p);
        self::assertTrue(RaiseFileSystem::pathExist($fp));
        RaiseFileSystem::deletePath($fp);
        self::assertFalse(RaiseFileSystem::pathExist($fp));

        $fp = RaisePath::combine(RaisePath::tempPath(), 'testPath/test2');
        $p = RaiseFileSystem::createPath($fp);
        self::assertTrue($p);
        self::assertTrue(RaiseFileSystem::pathExist($fp));
        $p = @RaiseFileSystem::deletePath(RaisePath::combine($fp, '..'));
        self::assertFalse($p);
        self::assertTrue(RaiseFileSystem::pathExist($fp));
        RaiseFileSystem::deletePath($fp);
        RaiseFileSystem::deletePath(RaisePath::combine($fp, '..'));
    }

    public static function testEmptyPath(){
        $fp = RaisePath::combine(RaisePath::tempPath(), 'testPath/test2/');
        $p = RaiseFileSystem::createPath($fp);
        self::assertTrue($p);
        self::assertTrue(RaiseFileSystem::pathExist($fp));

        $file1 = new RaiseFile('mocks/MockConfigFile.ini');
        $file1 = $file1->copy($fp);
        $file2 = new RaiseFile('mocks/MockRaiseUIControl.php');
        $file2 = $file2->copy($fp);

        self::assertTrue($file1->exists());
        self::assertTrue($file2->exists());

        $fp = RaisePath::combine($fp, '..');
        RaiseFileSystem::emptyPath($fp);

        self::assertFalse($file1->exists());
        self::assertFalse($file2->exists());
        
        self::assertFalse(RaiseFileSystem::pathExist(RaisePath::combine($fp, 'test2')));
        $p = RaiseFileSystem::deletePath($fp);
        self::assertTrue($p);
        self::assertFalse(RaiseFileSystem::pathExist($fp));
    }

    public static function testDiskSpace(){
        self::assertType(RaiseFileSystem::diskFreeSpace('/'), 'double');
        self::assertType(RaiseFileSystem::diskTotalSpace('/'), 'double');
        self::assertTrue((RaiseFileSystem::diskFreeSpace('/') / RaiseFileSystem::diskTotalSpace('/')) <= 1);
    }

}
